/**
 * PageMarket
 * TrandListAdapter.java
 * com.android.pagemarket.ui.adapter
 * 
 */
package com.android.pagemarket.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.pagemarket.R;
import com.android.pagemarket.business.logic.ContentManager;
import com.android.pagemarket.business.model.Message;
import com.android.pagemarket.ui.widget.PullToRefreshListView;
import com.android.pagemarket.ui.widget.PullToRefreshListView.OnRefreshListener;
import com.android.pagemarket.ui.widget.TrandItemView;
import com.android.pagemarket.util.NotifyListener;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-13
 * @see : 快讯适配器
 * @Copyright : copyrights reserved by personal 2007-2012
 **********************************************************/
public class TrandListAdapter extends DomoreListAdapter<Message> implements DataLoaderHandler<Message>
{
    /** 快讯或者促销的类型*/
    private int mCurType   = -1;
    private OnHeadMsgListenr headListenr;
    // 当前最大的消息的编号
    private int maxNoAlert = 0;
    private int maxNoPromo = 0;
    
    private ListNotifyListener alertListener;
    private ListNotifyListener promoListener;
    
    public TrandListAdapter(Context context, ListView listView)
    {
        super(context, listView, null);
        setDataLoaderHandler(this);
        
        if(listView instanceof PullToRefreshListView)
        {
            PullToRefreshListView pullListview = (PullToRefreshListView)listView;
            pullListview.setOnRefreshListener(new OnRefreshListener()
            {
                @Override
                public void onRefresh()
                {
                    loadData(null);
                }
            });
        }
    }

    public void setHeadListener(OnHeadMsgListenr listner)
    {
        headListenr = listner;
    }
        
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        if(convertView == null)
        {
            convertView = inflater.inflate(R.layout.listitem_trand, null);
        }
        TrandItemView view = (TrandItemView)convertView;
        Message item = (Message)getItem(position);
        view.bindData(item);
        
        return view;
    }

    // 载入快讯列表 促销列表 先从网络然后数据库
    public void loadData(Boolean isTrand)
    {        
        if(isTrand != null)
        {
            int t = isTrand ? Message.TYPE_ALERTS : Message.TYPE_PROMOT;
            if(mCurType == t && getCount() > 0)
            {
                // 如果是当前类型，而且已经有数据时，不重新加载数据，点获取更多来加载
                ContentManager.getInstance().updateReadedList(mList);
                ContentManager.getInstance().updateUnread(mList,mCurType);
                notifyDataSetChanged();
                return;
            }else
            {
                mCurType = t;
            }
        }
        setMaxNo( mCurType, 0);
        reload();
        showLoading();
        ContentManager.getInstance().getMessageList(mCurType,getMaxNo(mCurType), 10,getListListener(mCurType));
    }
    
    private void setMaxNo(int type, int maxNo)
    {
        if(Message.TYPE_ALERTS == type)
        {
            maxNoAlert = maxNo;
        }else
        {
            maxNoPromo = maxNo;
        }
    }
    
    private int getMaxNo(int type)
    {
        if(Message.TYPE_ALERTS == type)
        {
            return maxNoAlert;
        }else
        {
            return maxNoPromo;
        }
    }
    
    private NotifyListener<List<Message>> getListListener(int type)
    {
        if(Message.TYPE_ALERTS == type)
        {
            if(alertListener == null)
            {
                alertListener = new ListNotifyListener(Message.TYPE_ALERTS);
            }
            return alertListener;
        }else
        {
            if(promoListener == null)
            {
                promoListener = new ListNotifyListener(Message.TYPE_PROMOT);
            }
            return promoListener;
        }
    }
    
    /** 载入快讯列表 促销列表 从数据库
    public void loadDataDB(boolean isTrand)
    {
        type = isTrand ? Message.TYPE_ALERTS : Message.TYPE_PROMOT;
        page = 0;
        reload();
        showLoading();
        ContentManager.getInstance().getMessageListByDB(type, page, 10, listener);
    }*/
    
    @Override
    public int getMaxItems()
    {
        return 30;
    }

    @Override
    public void getNext(DataLoadedCallback<Message> callback)
    {
        ContentManager.getInstance().getMessageList(mCurType, getMaxNo(mCurType), 10, getListListener(mCurType));
    }
    
    @Override
    public boolean isLoading()
    {
        return false;
    }

    @Override
    public boolean haveNext()
    {
        return true;
    }
    
    private void filterHeader(List<Message> result)
    {
        if(headListenr != null && result!= null && result.size() > 0)
        {
            if(result.get(0).type == Message.TYPE_ALERTS)
            {
                boolean head = false;
                for(Message msg : result)
                {
                    if(msg.header)
                    {
                        head = true;
                        headListenr.onHeaderMsg(msg);
                        break;
                    }
                }
                if(!head)
                {
                    headListenr.onHeaderMsg(null);
                }
            }else
            {
                headListenr.onShow();
            }
        }
    }
    
    private class ListNotifyListener implements NotifyListener<List<Message>>
    {
        private int mType;
        
        public ListNotifyListener(int type)
        {
            mType = type;
        }
        
        @Override
        public void onSucceed(List<Message> result)
        {
            if(result!=null && result.size() > 0)
            {
                filterHeader(result);
                if(mCurType == mType)
                {
                    onLoaded(result);
                }
                ContentManager.getInstance().updateUnread(mList,mType);
                // 设定最后一个为最大的maxNO
                int maxNo = 0;
                for(Message msg : result)
                {
                    if(maxNo < msg.no)
                    {
                        maxNo = msg.no;
                    }
                }
                setMaxNo(mCurType, maxNo);
            }else
            {
                showDoMore();
                if(headListenr != null)
                {
                    headListenr.onShow();
                }
            }
        }
        
        @Override
        public void onError(int resultCode)
        {
            TrandListAdapter.this.onError(true);
            if(headListenr != null)
            {
                headListenr.onShow();
            }
        }
    };
   
    public interface OnHeadMsgListenr
    {
        public void onHeaderMsg(Message msg);
        
        public void onShow();
    }
}
